//
//  YYRegisterViewController.m
//  youyuanhuaxia
//
//  Created by 李刚 on 16/5/30.
//  Copyright © 2016年 CaoJIGen. All rights reserved.
//

#import "YYRegisterViewController.h"

@interface YYRegisterViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passworldTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation YYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //获得nib视图数组
    UIView *registerView = [[[NSBundle mainBundle]loadNibNamed:@"YYRegisterViewController" owner:self options:nil] lastObject];
    
//    [self.navigationController addChildViewController:self];
    
    //获得屏幕的Frame
    CGRect tmpFrame = [[UIScreen mainScreen] bounds];
    //设置自定义视图的中点为屏幕的中点
    [registerView setCenter:CGPointMake(tmpFrame.size.width, tmpFrame.size.height)];
    
    // 手势有效设置为YES  无效为NO
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    //给文本框添加事件
    [self.userNameTextField addTarget:self action:@selector(changeTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.passworldTextField addTarget:self action:@selector(changeTextField:) forControlEvents:UIControlEventEditingChanged];
    
    //每输入一个字符就变成点 用语密码输入
    self.passworldTextField.secureTextEntry = YES;
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    self.userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passworldTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
}

/**如果姓名和密码文本框都不为空时,让登陆按钮能点击*/
- (void)changeTextField:(UITextField *)textField {
    
    if (_userNameTextField.text.length > 0 && _passworldTextField.text.length > 0) {
        _registerBtn.enabled = YES;
    }else{
        _registerBtn.enabled = NO;
    }
}

//正则匹配电话号码
- (BOOL)checkTelNumber:(NSString *) telNumber{
    
    NSString *pattern = @"^1+[3578]+\\d{9}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    
    return isMatch;

}
//正则匹配用户密码 必须且只含有数字和字母，6-10位。
- (BOOL)checkPassword:(NSString*) password{
    
    NSString *pattern = @"/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    
    BOOL isMatch = [pred evaluateWithObject:password];
    
    return isMatch;
    
}

- (void)checkedUsernameAndPassworld:(NSString *)titleMassage {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:titleMassage preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *doneCancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:doneCancel];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


//点击注册按钮
- (IBAction)registerBtnClick:(UIButton *)sender {
    
    if (![self checkTelNumber:_userNameTextField.text]) {
        [self checkedUsernameAndPassworld:@"请输入正确的手机号码！"];
//        _userNameTextField.clearsOnBeginEditing = YES;
        
    }else if(![self checkPassword:_passworldTextField.text]) {
        [self checkedUsernameAndPassworld:@"用户密码只能是数字和字母组合，长度为6-10位"];
        _passworldTextField.clearsOnBeginEditing = YES;
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}


@end
